<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-24
 * Time: 14:31
 */

namespace plugin\csauto\app\service;


use plugin\csauto\app\model\RssModel;
use Shopwwi\LaravelCache\Cache;
use support\Log;

class ApiWorkService
{

    public static function setPostTorrentTaskDetails($item, $newData)
    {
//        echo "Url" . getenv('API_URL');
        // 使用专门的大数据提交方法，避免414错误
        $toolData = ToolService::curlPostDataLarge('https://auto-api.cspt.top/torrent/torrentTaskDetails', array(
            'site_id' => $item['site_id'],
            'torrent_id' => $item['id'],
            'torrent_view' => $newData,
            'token' => Cache::get(SystemService::USER_TOOL_TOKEN)
        ), array(), true);
        echo "ApiWorkService-setPostTorrentTaskDetails" . PHP_EOL;
//        print_r($toolData);
//        addlog('setPost',array(
//            'site_id' => $item['site_id'],
//            'torrent_id' => $item['id'],
//            'torrent_view' => $newData,
//            'token' => Cache::get(SystemService::USER_TOOL_TOKEN)
//        ));
        if (is_array($toolData)) return $toolData['data'];
        return false;
    }

    public static function torrentReleaseDetails($siteId, $torrentId, $toSiteId, $viewData)
    {
//        echo "Url" . getenv('API_URL');
        
        // 调试日志：记录发送到API的数据
        if (SystemService::getConfig(SystemService::ENABLE_RELEASE_DEBUG_LOG)) {
            $targetSiteName = isset($viewData['site_name']) ? $viewData['site_name'] : 'Unknown';
            $targetSiteKey = isset($viewData['site_key']) ? $viewData['site_key'] : 'Unknown';
            addlog('ApiWorkService-Debug-请求', "源站ID：{$siteId}，种子ID：{$torrentId} => 目标站点：{$targetSiteName}({$targetSiteKey})");
            addlog('ApiWorkService-Debug-请求', "发送的torrents_tag：" . json_encode($viewData['torrents_tag'] ?? 'NULL', JSON_UNESCAPED_UNICODE));
            addlog('ApiWorkService-Debug-请求', "发送的torrents_small_descr：" . ($viewData['torrents_small_descr'] ?? 'NULL'));
        }
        
        // 使用专门的大数据提交方法，避免414错误
        $toolData = ToolService::curlPostDataLarge('https://auto-api.cspt.top/torrent/torrentReleaseDetails', array(
            'site_id' => $siteId,
            'torrent_id' => $torrentId,
            'torrent_view' => json_encode($viewData),
            'to_site_id' => $toSiteId,
            'token' => Cache::get(SystemService::USER_TOOL_TOKEN)
        ), array(), true);
//        Log::info('torrentReleaseDetails', array(
//            'site_id' => $siteId,
//            'torrent_id' => $torrentId,
//            'torrent_view' => json_encode($viewData),
//            'to_site_id' => $toSiteId
//        ));
//        Log::info('torrentReleaseDetails-Data', $toolData);
//        print_r();
        // 
        if (is_array($toolData)) {
            // 调试日志：记录API响应
            if (SystemService::getConfig(SystemService::ENABLE_RELEASE_DEBUG_LOG)) {
                addlog('ApiWorkService-Debug-响应', "API响应状态码：" . ($toolData['code'] ?? 'NULL'));
                if (isset($toolData['data'])) {
                    addlog('ApiWorkService-Debug-响应', "响应的torrents_tag：" . json_encode($toolData['data']['torrents_tag'] ?? 'NULL', JSON_UNESCAPED_UNICODE));
                    addlog('ApiWorkService-Debug-响应', "响应的small_descr：" . ($toolData['data']['small_descr'] ?? 'NULL'));
                }
            }
            return $toolData['data'];
        }else{
            addlog('api-tool', $toolData, 'error');
            if (SystemService::getConfig(SystemService::ENABLE_RELEASE_DEBUG_LOG)) {
                addlog('ApiWorkService-Debug-响应', "API返回非数组数据", 'error');
            }
        }
//        print_r($toolData);
        return false;
    }


    public static function MediaInfoDetails($apiUrl, $torrentPath)
    {
        $toolData = ToolService::curlPostData($apiUrl, array(
            'torrent_path' => $torrentPath
        ), array(), true);
        if (!is_array($toolData)) return false;
        if ($toolData['code'] != 0) {
            Log::info('imgInfoDetails', $toolData);
            return false;
        }
        return $toolData['data'];
    }

    public static function cleanImg($apiUrl)
    {
        $toolData = ToolService::curlPostData($apiUrl, array(), array(), true);
        if (!is_array($toolData)) return false;
        if ($toolData['code'] != 0) {
            Log::info('imgInfoDetails', $toolData);
            return false;
        }
        return $toolData['data'];
    }

    public static function imgInfoDetails($apiUrl, $torrentPath, $timeout = null, $connectTimeout = null)
    {
        $toolData = ToolService::curlPostData($apiUrl, array(
            'file_url' => $torrentPath,
            'mode' => 'single',
            'format' => 'jpg',
            'count' => 3
        ), array(), true, $timeout, $connectTimeout);
        if (!is_array($toolData)) return false;
        if ($toolData['code'] != 0) {
            Log::info('imgInfoDetails', $toolData);
            return false;
        }
        return $toolData['data'];
    }


    public static function checkApi($apiUrl)
    {
        $toolData = ToolService::curlPostData($apiUrl, array(), array(), true);
        if (!is_array($toolData)) return false;
        if ($toolData['code'] != 0) return false;
        return true;
    }

    public static function SiteList()
    {
        $toolData = ToolService::curlPostData('https://auto-api.cspt.top/site/siteList', array(
            'token' => Cache::get(SystemService::USER_TOOL_TOKEN)
        ), array(), true);
        if (is_array($toolData)) return $toolData['data'];
        return false;
    }

    public static function updateAutoToken($data)
    {
        print_r($data);
        $toolData = ToolService::curlPostData('https://auto-api.cspt.top/auto/tokenUpdate', $data, array(), true);
        echo "updateAutoToken" . PHP_EOL;
//        print_r($toolData);

        if (is_array($toolData)) return $toolData['data'];
        return false;
    }
}